package cn.edu.swu;

import cn.edu.swu.common.PageTools;
import cn.edu.swu.modal.Book;
import cn.edu.swu.common.DBTools;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class MainServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Book> books= DBTools.getAllBook();
        StringBuilder sb=new StringBuilder();
        sb.append("<table class='tb-book'>");
        sb.append("<tr><th>编号</th><th>书名</th><th>作者</th><th>价格</th><th>内容</th></tr>");
        for(Book book : books){
            sb.append(String.format("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
            book.getId(),book.getName(),book.getAuthor(),book.getPrice(),book.getContent()));
        }
        sb.append("</table>");
        response.setContentType("text/html");
        try(Writer writer=response.getWriter()){
            writer.write(PageTools.wrap(sb.toString()));
        }
    }
}
